package org.sample.commons;

import java.math.BigDecimal;
import java.util.Scanner;

/**
 * Created by alan on 2017/6/11.
 */
public class StopWatch {

    private long startTime;

    public StopWatch() {
        this.startTime = System.currentTimeMillis();
    }

    public StopWatch(boolean stop) {
        if (stop)
            new Scanner(System.in).nextLine();
        System.out.println("开始");
        this.startTime = System.currentTimeMillis();
    }

    public long stop() {
        return System.currentTimeMillis() - startTime;
    }

    public String show() {
        long time = stop();
        if (time > 1000)
            return "耗时: " + new BigDecimal(time).divide(new BigDecimal(1000)).toEngineeringString() + "秒";
        else
            return "耗时: " + time + "毫秒";
    }

    public void print() {
        System.out.println(show());
    }

    public void print(String flag) {
        System.out.println(flag + show());
    }
}
